const { Controller } = require('egg')

class WorksController extends Controller {
  // 获得留言墙列表
  async getWorksList() {
    const { ctx, app } = this
    ctx.validate({
      page: { type: 'int', required: true, desc: '页码' },
      size: { type: 'int', required: false, desc: '每页条数' },
      tag: { type: 'string', required: false, desc: '标签' },
      userId: { type: 'string', required: true, desc: '用户id' }
    })
    const { page, size = 15, tag, userId } = ctx.request.body
    const whereReg = tag ? { tag, status: 1 } : { status: 1 }
    let result = await app.model.Works.findAndCountAll({
      where: whereReg,
      offset: (page - 1) * size,
      limit: size,
      order: [['id', 'DESC']],
      include: [
        {
          model: app.model.WorkComment,
          as: 'comments'
        },
        {
          model: app.model.WorksLike,
          as: 'likes'
        }
      ]
    })

    // 整理数据格式
    result = JSON.parse(JSON.stringify(result))
    result.rows.forEach((item) => {
      item.commentCount = item.comments.length
      item.likeCount = item.likes.length
      item.isLike = item.likes.some((item) => item.user_id === userId)
      delete item.likes
    })

    if (result) {
      return ctx.apiSuccess(result)
    }
    ctx.apiFail('获取留言墙列表失败')
  }

  // 发布新的便签
  async addWorks() {
    const { ctx, app } = this
    ctx.validate({
      username: {
        type: 'string',
        required: true,
        desc: '用户名'
      },
      message: {
        type: 'string',
        required: true,
        desc: '留言内容'
      },
      color_type: {
        type: 'string',
        required: true,
        desc: '颜色类型'
      },
      tag: {
        type: 'string',
        required: true,
        desc: '标签'
      },
      user_id: {
        type: 'string',
        required: true,
        desc: '用户id'
      }
    })

    const { username, message, color_type, tag, user_id } = ctx.request.body
    let result = await app.model.Works.create({
      username,
      message,
      color_type,
      tag,
      user_id
    })

    if (!result) {
      return ctx.apiFail('发布留言失败')
    }
    result = JSON.parse(JSON.stringify(result))
    result.commentCount = 0
    result.likeCount = 0
    result.isLike = false
    result.comments = []
    ctx.apiSuccess(result)
  }

  // 发表便签评论
  async addWorksComment() {
    const { ctx, app } = this
    ctx.validate({
      content: {
        type: 'string',
        required: true,
        desc: '评论内容'
      },
      works_id: {
        type: 'string',
        required: true,
        desc: '便签id'
      },
      username: {
        type: 'string',
        required: true,
        desc: '用户名'
      }
    })
    const { content, works_id, username } = ctx.request.body

    const res = await app.model.Works.findByPk(works_id)
    if (!res) {
      return ctx.apiFail('评论的便签不存在')
    }

    const result = await app.model.WorkComment.create({
      content,
      works_id,
      username,
      avatar_type: Math.floor(Math.random() * 4) + 1 // 1 -4
    })

    if (!result) {
      return ctx.apiFail('发表评论失败')
    }
    ctx.apiSuccess(result)
  }

  // 给便签点赞
  async addWorksLike() {
    const { ctx, app } = this
    ctx.validate({
      works_id: {
        type: 'string',
        required: true,
        desc: '便签id'
      },
      user_id: {
        type: 'string',
        required: true,
        desc: '用户id'
      }
    })
    const { works_id, user_id } = ctx.request.body
    const result = await app.model.WorksLike.create({
      works_id,
      user_id
    })
    if (!result) {
      return ctx.apiFail('点赞失败')
    }
    ctx.apiSuccess('点赞成功')
  }
}

module.exports = WorksController
